package com.itheima;

import java.util.Comparator;
import java.util.TreeSet;

public class TreeSetDemo {
    public static void main(String[] args) {
        //集合只能存储引用数据类型
        text2();
    }


    private static void text0() {
        TreeSet<Integer> set1 = new TreeSet<>();
        //包装成Integer对象--自动装箱
        set1.add(1);

        set1.add(5);
        set1.add(3);
        set1.add(2);
        set1.add(4);
        for (Integer integer : set1){
            System.out.println(integer);
        }
        /*System.out.println(set1);*/
    }

    public static void test1(){
        TreeSet<Student> set = new TreeSet<>();
        set.add(new Student("张三", 22));
        set.add(new Student("李四", 19));
        set.add(new Student("王五", 26));
        set.add(new Student("赵六", 20));

        for (Student student : set){
            System.out.println(student.getName() + "---" + student.getAge());
        }
    }
    public static void text2(){
/*        Comparator<com.itheima.Student> comparator = new Comparator<com.itheima.Student>() {
            @Override
            public int compare(com.itheima.Student o1, com.itheima.Student o2) {
                //如果是正序，则o1-o2
                //如果是倒序，则o2-o1
                return o1.getAge() - o2.getAge();
            }
        };*/
        TreeSet<Student> set = new TreeSet<>( (o1, o2) -> o1.getAge() - o2.getAge());
        set.add(new Student("张三", 22));
        set.add(new Student("李四", 19));
        set.add(new Student("王五", 26));
        set.add(new Student("赵六", 20));

        for (Student student : set){
            System.out.println(student.getName() + "---" + student.getAge());
        }
    }
    public static int sum(int...a){
        int total = 0;
        for (int i = 0; i < a.length; i++){
            total += a[i];
        }
        return total;
    }
}
